//永恒打卡，早安，晚安
import plugin from '../../../lib/plugins/plugin.js';
import fetch from 'node-fetch';
import moment from 'moment';

const sum = 4; // 限制打卡等次数避免刷屏
const dateTime = 'YYYY-MM-DD 00:00:00';

export class Example extends plugin {
  constructor() {
    super({
      event: 'message',
      priority: 5000, // 优先级，数字越小等级越高
      rule: [
        {
          reg: /^打卡$/,
          fnc: 'daka',
        },
        {
          reg: /^(早|早安|早喵|早安喵|早上好)$/,
          fnc: 'zao',
        },
        {
          reg: /^(晚安|晚安啦|晚安喵|晚安呐)$/,
          fnc: 'wan',
        },
      ],
    });
  }

  async daka(e) {
    console.log('用户命令：', e.msg);
    const data_redis = await redis.get(`daka${e.user_id}_qiandao`);
    let new_sum = 1;
    if (data_redis) {
      if (JSON.parse(data_redis)[0].num === sum) {
        e.reply(`今天已经打卡过${sum}次了请明天再来~`);
        return true;
      }
      new_sum += JSON.parse(data_redis)[0].num;
    }
    const time = moment(Date.now()).add('days', 1).format(dateTime);
    const new_date = (new Date(time).getTime() - new Date().getTime()) / 1000;
    const redis_data = [{ num: new_sum }];
    redis.set(`daka${e.user_id}_qiandao`, JSON.stringify(redis_data), {
      EX: parseInt(new_date),
    });
    const url = 'http://api.caonm.net/api/dm/index.php?';
    const msg = [
      '打卡成功帮你点赞!\n',
      segment.image(url),
    ];
    const qun = [
      segment.at(e.user_id),
      ' 打卡成功!这是给你的奖励!\n',
      segment.image(url),
    ];
    const type = e.message_type;
    if (type === 'private') {
      e.friend.thumbUp(e.user_id, 20);
      e.reply(msg);
    } else if (type === 'group' && !e.isMaster) {
      e.group.sendMsg(qun);
    }
    if (e.isMaster) {
      e.reply([
        '唔…主人今天看起来非常有精神呢！\n',
        segment.image(`http://api.caonm.net/api/dm/index.php?`),
        segment.image(`http://api.caonm.net/api/dm/index.php?`),
      ]);
    }
    return true;
  }

  async zao(e) {
    console.log('用户命令：', e.msg);
    const data_redis = await redis.get(`daka:${e.user_id}_zao`);
    let new_sum = 1;
    if (data_redis) {
      if (JSON.parse(data_redis)[0].num === sum) {
        e.reply([segment.at(e.user_id), `今天已经早安过${sum}次了请明天再来~`]);
        return true;
      }
      new_sum += JSON.parse(data_redis)[0].num;
    }
    const time = moment(Date.now()).add('days', 1).format(dateTime);
    const new_date = (new Date(time).getTime() - new Date().getTime()) / 1000;
    const redis_data = [{ num: new_sum }];
    redis.set(`daka:${e.user_id}_zao`, JSON.stringify(redis_data), {
      EX: parseInt(new_date),
    });
    const url = 'http://api.caonm.net/api/dm/index.php?';
    const msg = [
      '美好的一天又开始啦!给你点了小小的赞以示鼓励!\n',
      segment.image(url),
    ];
    const qun = [
      segment.at(e.user_id),
      ' 早上好!阁下昨晚睡得还好吗?\n',
      segment.image(url),
    ];
    const type = e.message_type;
    if (type === 'private') {
      e.friend.thumbUp(e.user_id, 5);
      e.reply(msg);
    } else if (type === 'group') {
      e.group.sendMsg(qun);
    }
    return true;
  }

  async wan(e) {
    console.log('用户命令：', e.msg);
    const data_redis = await redis.get(`daka:${e.user_id}_wan`);
    let new_sum = 1;
    if (data_redis) {
      if (JSON.parse(data_redis)[0].num === sum) {
        e.reply([segment.at(e.user_id), `你今天已经晚安过${sum}次了请明天再来~`]);
        return true;
      }
      new_sum += JSON.parse(data_redis)[0].num;
    }
    const time = moment(Date.now()).add('days', 1).format(dateTime);
    const new_date = (new Date(time).getTime() - new Date().getTime()) / 1000;
    const redis_data = [{ num: new_sum }];
    redis.set(`daka:${e.user_id}_wan`, JSON.stringify(redis_data), {
      EX: parseInt(new_date),
    });
    const msg = [
      '祝阁下好梦!甘雨已经帮你点赞了哟!\n',
      segment.image(`https://dev.iw233.cn/api.php?sort=random`),
    ];
    const qun = [
      segment.at(e.user_id),
      ' 祝阁下好梦!\n',
      segment.image(`https://dev.iw233.cn/api.php?sort=random`),
    ];
    const type = e.message_type;
    if (type === 'private') {
      e.friend.thumbUp(e.user_id, 5);
      e.reply(msg);
    } else if (type === 'group') {
      e.group.sendMsg(qun);
    }
    return true;
  }
}
